package com.example.aop.service;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.*;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.concurrent.Executors;

/**
 * ClassName:LogAspect
 * Package:com.example.aop.service
 * Description:
 *
 * @Author:ZhangXin
 * @Create:2023/5/10-22:53
 * @Version:v1.0
 */
@Component
@Aspect //切面类是需要使用@Aspect注解进行标注的
@Order(2)
public class LogAspect {//切面

    //切面 = 通知 + 切点

    //定义通用的切点表达式
    @Pointcut("execution(* com.example.aop.service..*(..))")
    public void 通用切点() {
    }

    //通知就是增强,就是具体的增强代码Advice,方法的形式出现

    //前置通知
    @Before("通用切点()")//前置通知
    public void beforeAdvise(JoinPoint joinPoint) {
        System.out.println("<======前置通知======>");
    }

    //后置通知
    @AfterReturning("execution(* com.example.aop.service.OrderService.*(..))")
    public void afterReturning(JoinPoint joinPoint) {
        System.out.println("<======后置通知======>");
        //获取目标方法的签名
        Signature signature = joinPoint.getSignature();
        //通过方法的签名可以获取到一个具体信息
        //获取目标方法的方法名
        String methodName = signature.getName();
        System.out.println(methodName);

    }

    //环绕通知(最大的范围,前置之前,后置之后)
    @Around("execution(* com.example.aop.service.OrderService.*(..))")
    public void aroundAdvice(ProceedingJoinPoint joinPoint) throws Throwable {
        //前面的代码
        System.out.println("<======前环绕======>");
        //执行目标
        joinPoint.proceed();//执行目标
        //后面的代码
        System.out.println("<======后环绕======>");
    }

    //异常通知
    @AfterThrowing("execution(* com.example.aop.service.OrderService.*(..))")
    public void afterThrowingAdvice(JoinPoint joinPoint) {
        System.out.println("<======异常通知======>");
    }

    //最终通知
    @After("execution(* com.example.aop.service.OrderService.*(..))")
    public void afterAdvice(JoinPoint joinPoint) {
        System.out.println("<======最终通知======>");
    }


}
